<?php
# ============================================================================
# 版权所有  Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/erphp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website : www.chilcoo.com
# 
# 文件信息:
# File : /sto/default.php
# 分类列表页面

# 载入常用集
# 导入common
require_once (dirname(__FILE__).'/../../include/common.inc.php');
require_once (dirname(__FILE__).'/../../include/_manager/login.inc.php');
# 页面级别
$conf_web_page_level = 0;

class page_index extends page{
	function __construct(){
		global $config;
		$this->database = new db(config::conf_db_host,config::conf_db_user,
			config::conf_db_password,config::conf_db_name);
	}
	function prod_listview(){
		$lv = null;
		try{
			$sql = "SELECT prodId,name,model 
				FROM cs2_prod ORDER BY model ASC;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			
		}
		return $lv;
	}
	function ship_listview(){
		$lv = null;
		try{
			$sql = "SELECT shipId,dates,priceJp 
				FROM cs2_ship ORDER BY dates DESC LIMIT 0, 3;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			
		}
		return $lv;
	}
	function listview(){
		$lv = null;
		try{
			$sql = "SELECT SUM(cs2_sto.price)/COUNT(cs2_sto.price) AS price,
				COUNT(cs2_sto.price) AS numbers,SUM(cs2_sto.price) AS sumprice,
				cs2_sto.shipId,cs2_sto.prodId,name,model,cs2_sto.stoId,IFNULL(
				(SELECT COUNT(cs2_slist.prodId) FROM cs2_slist WHERE cs2_sto.prodId = cs2_slist.prodId),0) AS slistcnt
				FROM cs2_sto 
				LEFT JOIN (cs2_ship,cs2_prod) ON (cs2_sto.shipId = cs2_ship.shipId AND cs2_sto.prodId = cs2_prod.prodId)
				GROUP BY (cs2_sto.prodId);";
			$lv = $this->database->select($sql);
		}catch (Exception $e){
			
		}
		return $lv;
	}
	function ajax_callback(){
		global $config;
		# POST 数据
		$token = $_POST['token'];
		# 初始返回用 json
		$json = array("ref"=>"error");

		if($token == config::TOKEN_ADD_STO){
			$dates = $_POST['dates'];
			$price = $_POST['price'];
			$number = $_POST['number'];
			$prodId = $_POST['prodId'];
			$shipId = $_POST['shipId'];
			$adminId = $_POST['adminId'];
			
			$sql = "INSERT INTO cs2_sto (dates, `price`, `prodId`, `shipId`,`adminId`) 
				VALUES";
			for($i=0;$i<$number;$i++){
				$sql=$sql."('".$dates."',".$price.",".$prodId.",".$shipId.",".$adminId.")";
				if($i<$number-1)
					$sql=$sql.",";
				else
					$sql=$sql.";";
			}
			try {
				$this->database->insert($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}if($token == config::TOKEN_DEL_STO){
			$prodId = $_POST['prodId'];
			$sql = "DELETE FROM cs2_sto WHERE (prodId=".$prodId.")";
			try {
				$this->database->delete($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}if($token == config::TOKEN_SELL_STO){
			$data = $_POST['data'];
			$adminId = $_POST['adminId'];
			
			$cnt = $data['default']['cnt'];
			$dates = $data['default']['dates'];
			$buyerId = $data['default']['userSn'];// 落札者ID =  datatable buyerId
			$fee = $data['default']['fee'];
			$tofee = $data['default']['tofee'];
			$post = $data['default']['post'];
			$address = $data['default']['address'];
			$name = $data['default']['name'];
			$tel = $data['default']['tel'];
			$sellguid = $config->guid();
			
			# $data['default']['dates']
			# 处理2个数据表操作

			try {
				$sql = "INSERT INTO cs2_sell (`dates`, `buyerId`, `fee`,`tofee`,`post`,`address`,`name`,`tel`,`sellguid`,`status`,`adminId`) 
					VALUES('".$dates."','".$buyerId."',".$fee.",".$tofee.",'".$post."','"
					.$address."','".$name."','".$tel."','".$sellguid."',0,".$adminId.")";
				
				$this->database->insert($sql);
				$sql = "INSERT INTO cs2_slist(`sellguid`, `prodId`, `auctionId`,`price`) 
					VALUES";
				
				for($i=0;$i<$cnt;$i++){
					$prodId = $data[$i]['prodId'];
					$auctionId = $data[$i]['auctionId'];
					$price = $data[$i]['price'];
					$sql=$sql."('".$sellguid."',".$prodId.",'".$auctionId."',".$price.")";
					if($i<$cnt-1)
						$sql=$sql.",";
					else
						$sql=$sql.";";
				}
				$this->database->insert($sql);
				
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		return json_encode($json);
	}
}

$pg = new page_index();
# Session 和 POST 安全访问.
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$_SESSION[config::conf_session_id_token] = $config->web_page_period;
	$config->smarty->assign('listview', $pg->listview());
	$config->smarty->assign('ship_listview', $pg->ship_listview());
	$config->smarty->assign('prod_listview', $pg->prod_listview());
	$config->smarty->assign('DATENOW',date("Y-m-d"));
	# $config->smarty->assign('len', $pg->len());
	# 加载页面
	$pg->render($config->temp_page_path.config::conf_web_template_extension);
}else{
	print_r($pg->ajax_callback());
}




?>